<?php
final class String
{
	function getPrefix($letter,$n)
	{
		$s="";
		for($i=0;$i<$n;$i++)
			$s.=$letter;
		return $s;
	}
	
	public function inArray($str, $arr=array())
	{
		if(!is_array($arr))
		{
			if($str == $arr) return 0;
		}elseif(count($arr) > 0)
		{
			foreach($arr as $key => $value)
			{
				if($str == $value) return $key;
			}
		}
		return -1;
	}
	
	public function matrixToArray($data,$col)
	{
		$arr = array();
		foreach($data as $item)
			$arr[]=$item[$col];
		return $arr;
	}
	
	public function array_Filter($data,$col,$value)
	{
		$arr = array();
		foreach($data as $item)
		{
			if($item[$col] == $value)
				$arr[]=$item;
		}
		return $arr;
	}
	
	public function swapArray($arr)
	{
		$ar=array();
		while(count($arr))
		{
			$ar[]=array_pop($arr);
		}
		return $ar;
	}
	
	public function numberFormate($num)
	{
		$arr = split("\.",$num);
		if($arr[1]!="")
			return number_format($arr[0], 0, '.', ',').".".$arr[1];
		else
			return number_format($arr[0], 0, '.', ',');
	}
	
	public function numberParse($str)
	{
		$str = str_replace(",","",$str);
		return number_format($str, 2, '.', '');
	}
	
	function arrayRemove($arr,$pos)
	{
		$ar=array();
		foreach($arr as $key => $item)
		{
			if($key!=$pos)
				$ar[$key]=$item;
		}
		return $ar;
	}
	
	function generateRandStr($length)
	{
		$randstr = "";
		for($i=0; $i<$length; $i++)
		{
			 $randnum = mt_rand(0,61);
			 if($randnum < 10){
				$randstr .= chr($randnum+48);
			 }else if($randnum < 36){
				$randstr .= chr($randnum+55);
			 }else{
				$randstr .= chr($randnum+61);
			 }
		}
		return $randstr;
   } 
	
	function findTag($tag,$closetag,$positon,$s)
	{
		$count=0;
		while($count < $positon)
		{
			
			$pos=strpos($s,$tag);
			$s=substr($s,$pos+strlen($tag));
			$count++;
		}
		$s=$this->findCloseTag($tag,$closetag,$s);
		return substr($s,strpos($s,">")+1);
		
	}
	
	function findCloseTag($tag,$closetag,$s)
	{
		$numtag=0;
		$find=strlen($s);
		for($i=0;$i<strlen($s);$i++)
		{
			$istag=false;
			$closetage=false;
			if($s[$i]==$tag[0])
			{
				$istag=true;
				for($j=0;$j<strlen($tag);$j++)
				{	
					if($s[$i+$j]!=$tag[$j])
						$istag=false;
				}
			}
			if($s[$i]==$closetag[0])
			{
				$closetage=true;
				for($j=0;$j<strlen($closetag);$j++)
				{
					if($s[$i+$j]!=$closetag[$j])
						$closetage=false;
				}
			}
			if($istag==true)
				$numtag++;
			if($closetage==true)
			{
				if($numtag > 0)
					$numtag--;
				else
				{
					$find=$i;
					break;
				}
			}
		}
		return substr($s,0,$find);	
	}
	
	function getSubString($string,$start,$end)
	{
		$poss = strpos($string,$start);
		$temp=substr($string,strpos($string,$start)+strlen($start));
		$pose=strpos($temp,$end) + strlen($start)+2;
		$s=substr($string,$poss,$pose-1);
		$s=str_replace($start,'',$s);
		$s=str_replace($end,'',$s);
		return $s;
	}
	
	
	function removeSubString($string,$start,$end)
	{
		$poss = strpos($string,$start);
		$temp=substr($string,strpos($string,$start)+strlen($start));
		$pose=strpos($temp,$end) + strlen($start)+2;
		return str_replace(substr($string,$poss,$pose-1),"",$string);
	}
	function removeAll($string,$start,$end)
	{
		while(strpos($string,$start)!== false)
		{
			$string=removeSubString($string,$start,$end);
		}
		return $string;
	}
	
	function isImage($ext)
	{
		$image = array('jpg','png','gif');
		if(in_array($ext,$image))
			return true;
		else
			return false;
	}
	
	function browser_info($agent=null) 
	{
		// Declare known browsers to look for
		$known = array('msie', 'firefox', 'safari', 'webkit', 'opera', 'netscape',
		'konqueror', 'gecko');
		
		// Clean up agent and build regex that matches phrases for known browsers
		// (e.g. "Firefox/2.0" or "MSIE 6.0" (This only matches the major and minor
		// version numbers.  E.g. "2.0.0.6" is parsed as simply "2.0"
		$agent = strtolower($agent ? $agent : $_SERVER['HTTP_USER_AGENT']);
		$pattern = '#(?<browser>' . join('|', $known) .
		')[/ ]+(?<version>[0-9]+(?:\.[0-9]+)?)#';
		
		// Find all phrases (or return empty array if none found)
		if (!preg_match_all($pattern, $agent, $matches)) return array();
		
		// Since some UAs have more than one phrase (e.g Firefox has a Gecko phrase,
		// Opera 7,8 have a MSIE phrase), use the last one found (the right-most one
		// in the UA).  That's usually the most correct.
		$i = count($matches['browser'])-1;
		return array($matches['browser'][$i] => $matches['version'][$i]);
	}
	
	function xmltoArray($objxml)
	{
		$data=array();
		$index=0;
		foreach($objxml->children() as $child)
		{
			foreach( $child->attributes() as $key => $item)
			{
				$data[$index][$key]= $item;
			}
			$data[$index]['xmlvalue'] = $child;
			$index++;
		}
		return $data;
	}
	
	function searchKey($ar,$att,$value)
	{
		foreach($ar as $item)
		{
			if($item[$att]==$value)
				return $item;
		}
		return false;
	}
	
	function referSiteMapToArray($refersitemap)
	{
		$refersitemap = str_replace("][","-",$refersitemap);
		$refersitemap = str_replace("[","",$refersitemap);
		$refersitemap = str_replace("]","",$refersitemap);
		$arr = split("-",$refersitemap);
		return $arr;
	}
	
	function getFileName($path)
	{
		$arr = split("/",$path);
		return $arr[count($arr)-1];
	}
}	
?>